<template>
  <div class="app-container">
    <!--<el-card class="operate-container" shadow="never">
      <el-form :inline="true" :model="listQuery" @submit.native.prevent label-width="110px">
        <el-form-item label="参数名称:" class="form-item">
          <el-input v-model="listQuery.name" class="filter-item" placeholder="部门负责人"/>
        </el-form-item>
        <el-form-item class="form-item">
          <el-button type="success" size="mini" icon="el-icon-search" @click="getList">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>-->
    <div class="table_content">
      <div class="btn-btm">
        <el-button type="primary" plain size="mini" @click="handleAdd">添加</el-button>
      </div>
      <!--表格渲染-->
      <el-table ref="table" :data="list" style="width: 100%;"
                :header-cell-style="{background: '#F8F8F9',padding: '8px 0 !important',color: '#515a6e'}">
        <el-table-column prop="name" label="参数名称"/>
        <!--<el-table-column prop="fieldName" label="参数键名"/>-->
        <el-table-column prop="value" label="参数键值">
          <template slot-scope="{row}">
            <div v-if="row.remark != 0 || row.remark != '' || row.remark != null">
              <span v-if="!row.editValue">{{{'0':'是','1':'否'}[row.value]}}</span>
              <el-radio-group v-if="row.editValue && (row.remark != 0 || row.remark != '')" v-model="row.value">
                <el-radio label="0">是</el-radio>
                <el-radio label="1">否</el-radio>
              </el-radio-group>
            </div>
            <div v-if="row.remark == '' || row.remark == 0 || row.remark == null">
              <span v-if="row.fieldName == 'mailPass' && !row.editValue">******</span>
              <span v-else-if="!row.editValue">{{row.value}}</span>
              <el-input v-if="row.editValue && (row.remark == 0 || row.remark == '')" v-model="row.value"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="{row}">
            <el-button class="btn" type="text" size="small" v-if="!row.editValue"
                       @click="handleEdit(row)">编辑</el-button>
            <el-button class="btn" type="text" size="small" v-if="row.editValue"
                       @click="cifDate(row)">确定</el-button>
            <el-button class="btn" type="text" size="small" v-if="row.editValue"
                       @click="colseEdit(row)">取消</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogPwdVisible"
               :close-on-click-modal="false" :show-close="false" width="35%">
      <el-form :model="editForm" label-width="140px" :rules="editFormRules" ref="editForm">
        <el-form-item label="参数名称" prop="name" class="form-item">
          <el-input v-model="editForm.name" placeholder="参数名称" class="filter-item"></el-input>
        </el-form-item>
        <el-form-item label="参数键名" prop="fieldName" class="form-item">
          <el-input v-model="editForm.fieldName" placeholder="参数键名"
                    :disabled="dialogStatus == 'update'" class="filter-item"></el-input>
        </el-form-item>
        <el-form-item label="参数键值" prop="value" class="form-item">
          <el-input v-model="editForm.value" placeholder="参数键值" class="filter-item"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="dialogPwdVisible=false">取消</el-button>
        <el-button type="primary" size="mini" @click="createRoleData">提交</el-button>
      </div>
    </el-dialog>

    <amend-pwd v-if="fistFlag == 0"></amend-pwd>
  </div>
</template>

<script>
  import { doPost, doGet, doFile, jsonPost } from '@/api/index'
  import Pagination from '@/components/Pagination/index'
  import { mapGetters } from 'vuex'

  export default {
    name: "config.vue",
    components: {
      Pagination
    },
    computed: {
      ...mapGetters([
        'name',
        'fistFlag',
        'userForm'
      ])
    },
    data() {
      return {
        list: [],
        editForm: {
          fieldName: '',
          name: '',
          value: ''
        },
        editFormRules: {
          name: [{ required: true, message: '配置名称不能为空', trigger: 'blur' }],
          fieldName: [{ required: true, message: '参数键名不能为空', trigger: 'blur' }],
          value: [{ required: true, message: '配置值不能为空', trigger: 'blur' }]
        },
        dialogPwdVisible: false,
        dialogStatus: '',
        textMap:{
          update: '编辑',
          create: '新增'
        },
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        doPost('config/list').then(res => {
          this.list = res.data
        }).catch(error => {
          this.list = []
        })
      },
      handleAdd() {
        this.dialogPwdVisible = true
        this.dialogStatus = 'create'
        this.editForm = {
          fieldName: '',
          value: '',
          name: ''
        }
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },
      handleEdit(row) {
        if (row) {
          for (var key in row) {
            if (row[key] == null || typeof(row[key]) === "undefined" || row[key] == undefined) {
              row[key] = ''
            }
          }
        }
        this.$set(row, "editValue", true)
        console.log('editValue',row.editValue)
        this.dialogStatus = 'update'
      },
      colseEdit(row) {
        row.editValue = false
        this.getList()
      },
      cifDate(row) {
        this.editForm = Object.assign({}, row)
        doPost(`config/update`, this.editForm).then(res => {
          if (res.code == 200) {
            this.getList()
            this.$message.success({
              message: '修改成功',
              type: 'success'
            })
          } else {
            this.$message.error(res.message)
          }
        }).catch(error => {
          this.$message.error(error.message)
        })
      },
      createRoleData() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doPost(`config/${this.dialogStatus == 'create'? 'add':'update'}`,this.editForm).then(res => {
              if (res.code == 200) {
                this.getList()
                this.dialogPwdVisible = false
                this.$message.success({
                  message: '设置成功',
                  type: 'success'
                })
              } else {
                this.$message.error(res.message)
              }
            }).catch(error => {
              this.$message.error(error.message)
            })
          }
        })
      },
    }
  }
</script>

<style lang="scss" scoped>

</style>
